package myClient.command;

import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * 命令的类型收集类
 */

@Data
@Service("ClientCommandMenu")
public class ClientCommandMenu implements BaseCommand {

    public static final String KEY = "0";
    private String allCommandsToShow;
    private String commandInput;

    @Override
    public String getKey() {
        return KEY;
    }

    @Override
    public String getTip() {
        return "show 所有命令";
    }

    @Override
    public void exec(Scanner scanner) {
        System.err.println("请输入某个操作指令：" + allCommandsToShow);
        //  获取第一个指令
        commandInput = scanner.next();
    }

    /**
     * <pre>
     * 设置命令说明，输出各个 command中的 tip
     * 字符串拼接形式 ： [menus] 0->show 所有命令 | 1->登录 | 2->聊天 | 3->退出 |
     * </pre>
     */
    public void setAllCommand(Map<String, BaseCommand> commandMap) {

        // Map.Entry 为 <key,value>键值对
        Set<Map.Entry<String, BaseCommand>> entries = commandMap.entrySet();
        //迭代器
        Iterator<Map.Entry<String, BaseCommand>> iterator = entries.iterator();
        //字符串拼接
        StringBuilder menus = new StringBuilder();
        menus.append("[menus] ");
        while (iterator.hasNext()) {
            BaseCommand tmpMenu = iterator.next().getValue();
            menus.append(tmpMenu.getKey())
                    .append("->")
                    .append(tmpMenu.getTip())
                    .append(" | ");
        }
        //控制台输出提示信息
        allCommandsToShow = menus.toString();
    }

}
